/*
 * DataBase.java
 *
 * Created on 2007��9��10��, ����3:57
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package orest.common.util;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;



/**
 *
 * @author yan dongdong
 */
public class DataBase{
    
    
    private Connection conn       = null;        /* define the database connection object*/    
    private Statement stm          = null;          /* statement is used to excute sql process */
    private PreparedStatement pstm = null;          /* build the sql string */
    private ResultSet rs           = null;          /* save the result */
    private int iPoolNumberCount   = 0;      /* save the count of connection */
    
    /** Creates a new instance of DataBase */
    public DataBase(){
        
    }
    
    /** 
     * description: print how many connection in the pool  
     *       input: null
     *      output: null
     *        date: 2007-09-10 
     */
    public void showNumber()
    {
        String sResult = new Tools().getDateTime1 ();
        System.out.print("Now, there are " + Integer.toString(iPoolNumberCount) +
                          " connections in the pool.");
    }
    
 
    public Connection getSysConnectionPool() throws Exception
    {
        Context envContext = null;                 /* It's a factory class to find the object that 
                                                       will save the database driver */
        Context drvContext = null;                 /* We will use the object to find the database 
                                                       driver which was writed in xml.cfg */
        DataSource ds = null;                      /* datasource object used to get the connection */
        
        try{
            /* if the connection is */
            if(null != conn){
                System.out.println("The database is connected !");
                return conn;
            }
            try{         
                /* get the database driver */
                envContext = new InitialContext();
                drvContext = (Context)envContext.lookup("java:comp/env");
                ds = (DataSource)drvContext.lookup("jdbc/SSTA");
                //除tomcat以外获取数据源的方式：
//                envContext = new InitialContext();
//                ds = (DataSource)envContext.lookup("jdbc/SSTA");
                iPoolNumberCount++;
            } 
            catch(NamingException e){
                System.out.println("Something unexpected happened when get database connection from pool. " +
                                    "Cannot to retrieve the java:comp/env/jdbcsql. " + e);
            }
            
            /* create the database connection */
            conn = ds.getConnection();
            
            /* show how many connection in the pool */
            showNumber();
            
            return conn;
        }        
        catch(SQLException e){
            System.out.println("Something unexpected happened when get database connection from pool. " + e);
            return conn;
        }/*
        finally {
            try {
                if (conn != null)
                    conn.close();
            } 
            catch (SQLException e) {
                
                System.out.print ("When close the connection, there are some exception! ");
            }
        }*/

    }
    

  
    
    
    
}
